<?php

namespace Frieza\Modules\Position\Controllers;

use App\Http\Controllers\Controller;
use Doraemon\model\Position\PositionCarousel;
use Doraemon\pockets\constInterface\LinkConf;
use Doraemon\service\position\AdmExtOperationModule;
use Doraemon\service\position\PositionModule;
use Encore\Admin\Grid;
use Encore\Admin\Widgets\Form;
use Illuminate\Http\Request;


class HomeFollowCarouselController extends BaseController
{
    use BaseTrait;

    public function saveModule(Request $request, $baseInfoId)
    {
        $hot = $request->input('hot', PositionCarousel::ICON_NONE);
        $imgLink = $request->input("base.link", []);
        $link_type = $imgLink['link_type'] ?? '';
        $link_params = isset($imgLink['link_params']) ? $imgLink['link_params'] : [];
        $data['topic_id'] = $link_params['topic_id'] ?? 0;
        if ($link_type != LinkConf::LINK_TOPIC_DETAIL) {
            throw new \Exception('只允许选择话题类型');
        }
        if ($data['topic_id'] <= 0) {
            throw new \Exception('话题id必填');
        }
        $data['position_base_info_id'] = $baseInfoId;
        $data['icon'] = $hot;
        $data['topic_id'] = $link_params['topic_id'] ?? 0;
        AdmExtOperationModule::addPositionCarousel($data);
    }

    public function beforeLinkForm(Form $form)
    {
        $seed = range(1, 15);
        $form->select('base.pos', '展示位置')->options(array_combine($seed, $seed));
        $form->radio('hot', '热度标识')->options(PositionCarousel::ADM_ICON_MAP)->default(PositionCarousel::ICON_HOT);
    }

    public function defaultBaseInfo(Request $request)
    {
        $response = parent::defaultBaseInfo($request);

        $response['link']['link_type'] = LinkConf::LINK_TOPIC_DETAIL;
        return $response;
    }

    public function extendList(Grid $grid)
    {
        $grid->column('topic_title', '话题标题');
        $grid->column('hot', '热度标识');
        $grid->column('pos', '展示位置');
    }

}
